<?php
class Url {
	
	private $arrSeccion = array();
	private $strSeccion = "";
	private $arrParams = array();
	private $strParams = "";
	
	function __construct() {
		global $parametros;
		
		$this->strSeccion = $_GET["qrystr"];
		
		$arr1 = explode("/",$this->strSeccion);
		
		foreach ($arr1 as $elm1) {
			unset($seccion);
			$aux2 = explode("-", $elm1);
			switch (count($aux2)) {
				case 3:
					$seccion["ids"] = $aux2[2];
				case 2:
					$seccion["accion"] = $aux2[1];
				case 1:
					$seccion["controlador"] = $aux2[0];
					break;
			}
			$this->arrSeccion[] = $seccion;
		}

		$arr = $_GET;
		unset($arr["qrystr"]);
		
		$separador = "?";
		foreach ($arr as $clave => $valor) {
			$this->strParams.=$separador.$clave."=".$valor;
			$separador = "&";
		}
	}
	
	public function ruta() {
		return __SITE_HOST."/".$this->strSeccion.$this->strParams;
	}
	
	public function cambia($clave, $valor, $seccion = false) {
		global $parametros;

		if ($seccion == true) {
			$arr = $this->arrSeccion;
			
			if (is_null($valor)) {
				unset($arr[count($arr)-1][$clave]);
			} else {
				$arr[count($arr)-1][$clave] = $valor;
			}
			$this->arrSeccion = $arr;
			$this->strSeccion = $this->rutaSeccion();
		} else {
			
		}
	}
	
	public function adelante() {
		$this->arrSeccion[] = array();
		$this->strSeccion = $this->rutaSeccion();
	}
	
	public function atras() {
		array_pop($this->arrSeccion);
		$this->strSeccion = $this->rutaSeccion();
	}
	
	private function rutaSeccion() {
		$seccion = "";
		
		$separador = "";
		foreach($this->arrSeccion as $clave => $valor) {
			//Util::depura($valor);
			if (isset($valor["controlador"])) {
				$seccion.= $separador.$valor["controlador"];
				if (isset($valor["accion"])) {
					$seccion.="-".$valor["accion"];
					if (isset($valor["ids"])) {
						$seccion.="-".$valor["ids"];
					}
				}
				$separador = "/";
			}
		}
		
		return $seccion;
	}
	
	public function control() {
		$control = null;
		
		foreach ($this->arrSeccion as $seccion) {
			$control[$seccion["controlador"]] = $seccion;
		}	
		
		return $control;
	}
	
}
?>